package com.wjj.servlets;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.afs.model.v20180112.AuthenticateSigRequest;
import com.aliyuncs.afs.model.v20180112.AuthenticateSigResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 作者：wangjj
 * @version 版本：
 * @createTime 创建时间：2018-05-07 14:11
 * @discription 类说明：login_servlet
 */
public class LoginServlet extends HttpServlet{

    private static final String regionid = "cn-hangzhou";
    private static final String accessKeyId = "";
    private static final String accessKeySecret = "";
    private static final String appkey = "";

    private static final IClientProfile profile= DefaultProfile.getProfile(regionid, accessKeyId, accessKeySecret);


    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setHeader("content-type", "text/html;charset=UTF-8");
        PrintWriter writer = resp.getWriter();

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String sessionId = req.getParameter("sessionid");
        String sig = req.getParameter("sig");
        String token = req.getParameter("token");
        String scene = req.getParameter("scene");

        // 登入IP 自己系统代码
        //String clientIp = SysUtil.getClientIp(argRequest);
        // 访问IP
        String remoteIp = req.getRemoteAddr();
        // 使用代理
        if (req.getHeader("x-forwarded-for") != null) {
            remoteIp = req.getHeader("x-forwarded-for");
        }

        int result = test(sessionId, sig, token, scene, appkey, remoteIp);
        if(result == 100) {
            System.out.println("验签通过");
            if ("wjj".equals(username) && "123456".equals(password)) {
                writer.print("登录成功！");
            } else {
                writer.print("登录失败！");
            }
        } else {
            System.out.println("验签失败");
        }

    }

    public int test(String sessionId, String sig, String token, String scene, String appkey, String remoteIp){
        // Create a new IClientProfile instance
        IAcsClient client = new DefaultAcsClient(profile);
        int result = 0;
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "afs", "afs.aliyuncs.com");

            AuthenticateSigRequest request = new AuthenticateSigRequest();
            request.setSessionId(sessionId);// 必填参数，从前端获取，不可更改，android和ios只传这个参数即可
            request.setSig(sig);// 必填参数，从前端获取，不可更改
            request.setToken(token);// 必填参数，从前端获取，不可更改
            request.setScene(scene);// 必填参数，从前端获取，不可更改
            request.setAppKey(appkey);// 必填参数，后端填写
            request.setRemoteIp(remoteIp);// 必填参数，后端填写

            AuthenticateSigResponse response = client.getAcsResponse(request);
            result = response.getCode();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

}
